package com.cl.core.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.core.dto.PagingReqDTO;
import com.cl.core.mapper.PlatformMenusMapper;
import com.cl.core.pojo.PlatformMenus;
import com.cl.core.service.MenuServices;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author liddhappy
 * @description menuServices 实现
 * @createTime 2022/12/3 21:05
 */
@Service
@Slf4j
public class MenuServiceImpl extends ServiceImpl<PlatformMenusMapper, PlatformMenus> implements MenuServices {

    @Autowired
    PlatformMenusMapper menusMapper;

    @Override
    public void addMenu(PlatformMenus menus) {
        log.info("add menu " + menus);
        menusMapper.insertSelective(menus);
        log.info("add menu success");
    }

    @Override
    public void deleteMenu(String menuId) {
        log.info("deleteMenuByMenuId " + menuId);
        Long convert = !StringUtils.isEmpty(menuId) ? (Long) ConvertUtils.convert(menuId, Long.class) : Long.MIN_VALUE;
        menusMapper.deleteByPrimaryKey(convert);
        log.info("deleteMenuByMenuId  success");
    }

    @Override
    public void updateMenu(PlatformMenus menus) {
        log.info("updateMenu  menu " + menus);
        PlatformMenus platformMenus = menusMapper.selectByPrimaryKey(menus.getMenuId());
        BeanUtils.copyProperties(menus, platformMenus);
        menusMapper.updateByPrimaryKey(platformMenus);
        log.info("updateMenu  success");
    }

    @Override
    public PlatformMenus selectMenu(String menuId) {
        log.info("selectMenu  menu " + menuId);
        Long convert = !StringUtils.isEmpty(menuId) ? (Long) ConvertUtils.convert(menuId, Long.class) : Long.MIN_VALUE;
        PlatformMenus platformMenus = menusMapper.selectByPrimaryKey(convert);
        log.info("selectMenu  result  " + platformMenus);
        log.info("selectMenu  success");
        return platformMenus;
    }

    /**
     * @param reqDTO 分页查询请求
     * @return 考试对象
     */
    @Override
    public IPage<PlatformMenus> paging(PagingReqDTO<PlatformMenus> reqDTO) {

        //创建分页对象
        Page page = new Page(reqDTO.getCurrent(), reqDTO.getSize());

        //转换结果
        return baseMapper.paging(page, reqDTO.getParams());
    }


}
